<template>
    <div>
        <mixinsObj></mixinsObj>
        <el-card class="box-card">
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入内容" class="input-with-select" v-model="obj.query" clearable
                        @clear="getTableDate()">
                        <el-button slot="append" icon="el-icon-search" @click="getTableDate()"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="3">
                    <el-button type="primary" @click="dialogFormVisible = true">添加用户</el-button>
                </el-col>
            </el-row>
            <el-table :data="tableData" border style="width: 100%">
                <el-table-column fixed type="index" label="#">
                </el-table-column>
                <el-table-column prop="username" label="姓名">
                </el-table-column>
                <el-table-column prop="email" label="邮箱">
                </el-table-column>
                <el-table-column prop="mobile" label="电话">
                </el-table-column>
                <el-table-column prop="role_name" label="角色">
                </el-table-column>
                <el-table-column label="状态">
                    <template slot-scope="scope">
                        <el-switch active-color="#13ce66" inactive-color="#ff4949" v-model="scope.row.mg_state"
                            @change="editSwitch(scope.row)"> </el-switch>
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作" width="200px">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row.id)">
                        </el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="del(scope.row.id)">
                        </el-button>
                        <el-button type="warning" icon="el-icon-star-off" size="mini"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="obj.pagenum" :page-sizes="[2, 3, 4, 5]" :page-size="obj.pagesize"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
            <!-- <pagination 
            :total="total" 
            :pageSize="obj.pagesize" 
            :pageSizes="[2, 3, 4, 5]" 
            @sizeChange="handleSizeChange" 
            @currentChange="handleCurrentChange"
            :current-page="obj.pagenum" >
            </pagination> -->

        </el-card>
        <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
            <el-form :model="form" :rules="fromRules" ref="fromRef">
                <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
                    <el-input v-model="form.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
                    <el-input v-model="form.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
                    <el-input v-model="form.email" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="电话" :label-width="formLabelWidth" prop="mobile">
                    <el-input v-model="form.mobile" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="add">确 定</el-button>
            </div>
        </el-dialog>




        <el-dialog title="修改用户" :visible.sync="editFormVisible">
            <el-form :model="editform" :rules="fromRules" ref="editfromRef">
                <el-form-item label="用户名" :label-width="editformLabelWidth" >
                    <el-input v-model="editform.username" autocomplete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="邮箱" :label-width="editformLabelWidth" prop="email">
                    <el-input v-model="editform.email" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="电话" :label-width="editformLabelWidth" prop="mobile">
                    <el-input v-model="editform.mobile" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="editUser()">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import mixinsObj from '@/views/mixins.vue'
import pagination from '@/views/pagination.vue'
import qs from 'querystring'
export default {
    components: { mixinsObj, pagination },
    data() {
        let checkAge = (rule, value, callback) => {
            let reg = /^1[3-9]\d{9}$/;
            if (reg.test(value)) {
                callback();
            } else {
                callback("手机号不正确");
            }
        };
        return {
            tableData: [],
            obj: {
                query: '',
                pagenum: 1,
                pagesize: 2
            },
            total: 0,
            dialogTableVisible: false,
            dialogFormVisible: false,
            editFormVisible: false,
            formLabelWidth: '120px',
            editformLabelWidth: '120px',
            form: {
                username: '',
                password: '',
                email: '',
                mobile: '',
            },
            editform: {
                username: '',
                email: '',
                mobile: '',
            },
            fromRules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                mobile: [
                    { required: true, message: '请输入电话号', trigger: 'blur' },
                    { validator: checkAge, message: '请输入正确的电话号', trigger: 'blur' }
                ],
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
                ],
            }
        }
    },
    methods: {
        async getTableDate() {
            let { data } = await this.$http.get('users?' + qs.stringify(this.obj))
            console.log(data);
            this.tableData = data.data.users
            this.total = data.data.total
        },
        handleSizeChange(val) {
            this.obj.pagesize = val
            this.getTableDate()
        },
        handleCurrentChange(val) {
            console.log(val);
            this.obj.pagenum = val
            this.getTableDate()
        },
        add() {
            this.$refs.fromRef.validate(async (valid) => {
                this.dialogFormVisible = false
                if (valid) {
                    let { data } = await this.$http.post('users', this.form)
                    if (data.meta.status == 201) {
                        this.$message.success(data.meta.msg)
                        this.getTableDate()
                    } else {
                        this.$message.error(data.meta.msg)
                    }
                    this.form = {
                        username: '',
                        password: '',
                        email: '',
                        mobile: '',
                    }

                }
            });

        },
        del(key) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                let { data } = await this.$http.delete('users/' + key)
                if (data.meta.status == 200) {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }
                this.getTableDate()

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });


        },
        async editSwitch(uid) {
            this.value = !this.value
            let { data } = await this.$http.put(`users/${uid.id}/state/${uid.mg_state}`)
            if (data.meta.status == 200) {
                this.$message.success(data.meta.msg)
            } else {
                this.$message.error(data.meta.msg)
            }
        },
        async edit(id) {
            let { data } = await this.$http.get('users/' + id)
            this.editform = data.data
            this.editFormVisible = true
        },
        editUser() {
            this.$refs.editfromRef.validate(async (valid) => {
                this.editFormVisible = false
                if (valid) {
                    let { data } = await this.$http.put('users/' + this.editform.id, this.editform)
                    if (data.meta.status == 200) {
                        this.$message.success(data.meta.msg)
                        this.getTableDate()
                    } else {
                        this.$message.error(data.meta.msg)
                    }
                    this.editform = {
                        username: '',
                        password: '',
                        email: '',
                        mobile: '',
                    }
                }
            });

        }
    },
    mounted() {
        this.getTableDate()
    }
}
</script>